(function() {
  app.controller('withdrawCtrl', function($rootScope, $interval, $scope, $state, tradeApi, alertTip, $window) {
    $scope.balance_out = {};
    $scope.selected = {};
    $scope.mobile = sessionStorage.getItem('mobile');
    $scope.getList = function() {
      tradeApi.query({
        funcId: '300006',
        data: {}
      }).then(function(res) {
        if (!res || res.length == 0) {
          alertTip.open({
            state: 0,
            msg: '请先绑定银行卡！'
          });
          return;
        }
        $scope.sele_title = '选择到账银行卡';
        $scope.confirmview = true;
        $scope.type = 'bank_name';
        $scope.sourceObj = [];
        angular.forEach(res, function(item) {
          var obj = {};
          obj.text = item.bank_name + ' ' + item.display_bank_no;
          obj.param = item.bank_id;
          $scope.sourceObj.push(obj);
        });
        $scope.close_flag = true;
      });
    };

    $scope.init = function() {
      tradeApi.query({
        funcId: '300004',
        data: {}
      }).then(function(res) {
        $scope.balance = res.enable_balance;
      });
    };

    var num = 60;
    var count = 0; // 防止多次点击
    $scope.get_code_color = 1;
    $scope.code_tip = '获取验证码';
    $scope.client = {};

    $scope.get_code = function() {
      count++;
      if (count !== 1) { return; }
      tradeApi.query({
        funcId: '300021',
        data: {
          auth_type: '2'
        }
      }).then(function(res) {
        $scope.get_code_color = 0;
        var timer = $interval(function() {
          num--;
          $scope.code_tip = num + '秒后重新获取';
        }, 1000, 59).then(function() {
          $scope.get_code_color = 1;
          $scope.code_tip = '重新获取验证码';
          num = 60;
          count = 0;
        });
      });
    };

    $scope.submit = function() {
      $scope.balance_out.bank_id = $scope.selected.param;
      tradeApi.query({
        funcId: '300014',
        data: $scope.balance_out
      }).then(function(res) {
        alertTip.open({
          state: 1,
          msg: '出金申请成功'
        });
        $window.history.back();

        // $scope.balance_out = {};
        // $scope.init();
      });
      $scope.xieyi_ = false;
    };
  });
})();
